package com.gxzy.domain.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gxzy.config.FinalPoolCfg;
import com.gxzy.domain.base.BaseParent;
import com.gxzy.util.convert.MapAndStringConvert;
import com.gxzy.util.convert.PwdEncryptionConvert;
import com.gxzy.util.validator.PasswordBase;
import com.gxzy.util.validator.PhoneBase;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashMap;

/**
 * 登录信息
 */

@Entity
@Data
@ToString(callSuper = true)
@Builder
@NoArgsConstructor                 //无参构造
@AllArgsConstructor                //有参构造
@EqualsAndHashCode(callSuper = true)
@Table(name = FinalPoolCfg.TB_LOGIN_NAME)
@org.hibernate.annotations.Table(appliesTo = FinalPoolCfg.TB_LOGIN_NAME, comment = "登录信息")
public class Login extends BaseParent implements Serializable {
    private static final long serialVersionUID = 1L;

    @NotBlank(message = "用户名不能为空")
    @Size(min = 2, max = 50, message = "用户名长度应该在2-50之间")
    @Column(columnDefinition = "VARCHAR(50) COMMENT '用户名'", unique = true)
    private String username;                                // 用户名

    @NotBlank(message = "手机号不能为空")
    @PhoneBase
    @Column(nullable = false, columnDefinition = "VARCHAR(11) COMMENT '手机号'", unique = true)
    private String phone;                                   // 手机号

    @Email(message = "邮箱格式错误")
    @Column(columnDefinition = "VARCHAR(50) COMMENT '邮箱'")
    private String email;                                   // 邮箱

    @NotBlank(message = "密码不能为空")
    @Size(min = 6, max = 50, message = "密码长度应该在6-50之间")
    @PasswordBase(message = "密码需要包含至少一个英文及数字")
    @Column(nullable = false, columnDefinition = "VARCHAR(128) COMMENT '密码'")
    @Convert(converter = PwdEncryptionConvert.class)
    @JsonIgnore
    private String password;                                // 密码

    @Column(columnDefinition = "BIGINT(20) COMMENT '角色外键'")
    private Long role;                                      // 角色外键

    @Column(columnDefinition = "TEXT COMMENT '权限组'")
    @Convert(converter = MapAndStringConvert.class)
    private HashMap<String, Boolean> identity;              // 权限组
}
